package com.tpshion.deploy.util;

import com.tpshion.deploy.common.support.BusinessException;
import com.tpshion.deploy.common.constant.ErrorCode;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

public class AssertUtil {

    private AssertUtil() {

    }

    public static void isEmpty(Object obj, String msg){
        isEmpty(obj, ErrorCode.FAIL, msg);
    }

    public static void isEmpty(Object obj, ErrorCode errorCode){
        isEmpty(obj, errorCode, errorCode.getMsg());
    }

    public static void isEmpty(Object obj, ErrorCode errorCode, String msg){
        if(ObjectUtils.isEmpty(obj)){
            throw new BusinessException(errorCode.getCode(), msg);
        }
    }

    public static void isBlank(String value, String msg){
        isBlank(value, ErrorCode.FAIL, msg);
    }

    public static void isBlank(String value, ErrorCode errorCode){
        isBlank(value, errorCode, errorCode.getMsg());
    }

    public static void isBlank(String value, ErrorCode errorCode, String msg){
        if(StringUtils.hasText(value)){
            return;
        }
        throw new BusinessException(errorCode.getCode(), msg);
    }

    public static void isTrue(boolean flag, String msg){
        isTrue(flag, ErrorCode.FAIL, msg);
    }

    public static void isTrue(boolean flag, ErrorCode errorCode){
        isTrue(flag, errorCode, errorCode.getMsg());
    }

    public static void isTrue(boolean flag, ErrorCode errorCode, String msg){
        if(flag){
            throw new BusinessException(errorCode.getCode(), msg);
        }
    }

    public static void isFalse(boolean flag, String msg){
        isFalse(flag, ErrorCode.FAIL, msg);
    }

    public static void isFalse(boolean flag, ErrorCode errorCode){
        isFalse(flag, errorCode, errorCode.getMsg());
    }

    public static void isFalse(boolean flag, ErrorCode errorCode, String msg){
        if(flag){
            return;
        }
        throw new BusinessException(errorCode.getCode(), msg);
    }
}
